// pages/mine/myCoupons/myCoupons.js
import api from "../../../common/api.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    couponsList: [
    //   {
    //   type: '折扣券',
    //   startDate: '2018.03.11',
    //   endDate: '2018.04.11',
    //   title: '新人首购无门槛券',
    //   status: '0',
    //   canUse: 'YEAR,HALFYEAR,QUARTER,MONTH',
    //   discount: 7,
    //   days: 0,
    // },{
    //   type: '会员期赠送',
    //   startDate: '2018.03.11',
    //   endDate: '2018.03.20',
    //   title: '续费季卡会员10元券',
    //   status: '0',
    //   canUse: 'QUARTER,MONTH',
    //   discount: 0,
    //   days: 5,
    // },{
    //   startDate: '2018.03.11',
    //   endDate: '2018.03.20',
    //   useDate: '2018.04.11 18:00:00',
    //   title: '续费季卡会员10元券',
    //   status: '1',
    //   canUse: 'QUARTER,MONTH',
    //   discount: 0,
    //   days: 5,
    // },{
    //   startDate: '2018.03.11',
    //   endDate: '2018.03.20',
    //   title: '续费季卡会员10元券',
    //   status: '2',
    //   canUse: 'QUARTER,MONTH',
    //   discount: 0,
    //   days: 5,
    // }
  ],
  show: false,
  },
  getMyMemberCoupons(){
    let that = this;
    return new Promise((resolve, reject)=>{
      api.getMyMemberCoupons({}, res => {
        try {
          let content = res.data.content;
          content.map(v=>{
            v.remainDays = Math.ceil((v.endDate-(new Date().getTime()))/(24*60*60*1000)) 
            v.couponContent = JSON.parse(v.couponContent)
            v.startDate = that.formatDateTime(v.startDate)
            v.endDate = that.formatDateTime(v.endDate)
            let l = v.cardType.split(',').length - 1
            if(l==3){
              v.canUseTypeText = '购买任意会员均适用'
            }else {
              v.canUseTypeText = '购买'+v.cardType.replace('HALFYEAR','半年卡').replace('YEAR','年卡').replace('QUARTER','季卡').replace('MONTH','月卡')+'会员适用'
            }
          })
          that.setData({
            couponsList: content,
            show: true
          })
        } catch (error) {
          
        }
      })
    })
  },
  formatDateTime(timeStamp){
    var date = new Date(timeStamp);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;
    //day 有值-返回带分秒的
      return y + '.' + m + '.' + d
  },  
  goToMember(e){
    let type = e.target.dataset.type;
    let couponId = e.target.dataset.id;
    type = type.split(',')[0]

    api.getMyMemberCouponsInfo({},res=>{
      let content = res.data.content;
      let selectedCoupon;
      content[type].map((v, index) => {
        if(v.id ==couponId){
          selectedCoupon = index
        }
      })
      wx.navigateTo({
        url: '/pages/mine/memberCentre/memberCentre?type='+type+'&couponId='+couponId+'&selectedCoupon='+selectedCoupon,
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    getApp().globalData.loginPromise.then(() => {
      this.getMyMemberCoupons()
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})